package global.com.loansapplication.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.ui.actviity.user.biz.ResetPwdBiz;
import global.com.loansapplication.ui.actviity.user.component.DaggerResetPwdComponent;
import global.com.loansapplication.ui.actviity.user.contract.ResetPwdContract;
import global.com.loansapplication.ui.actviity.user.module.ResetPwdModule;
import global.com.loansapplication.ui.actviity.user.presenter.ResetPwdPresenter;
import global.com.loansapplication.util.TimerUtil;
import rx.functions.Action1;

/**
 * Created by admin on 2017/12/2.
 */

public class ResetPwdAct extends BaseActivity implements ResetPwdContract.View {


    @Bind(R.id.suggetion_submit)
    TextView submit;
    @Bind(R.id.pwd_old)
    EditText phone;
    @Bind(R.id.register_code_edit)
    EditText code;
    @Bind(R.id.register_get_code)
    TextView getCode;
    @Bind(R.id.pwd_new)
    EditText pwd;
    @Bind(R.id.pwd_new_sure)
    EditText pwdSure;

    @Inject
    ResetPwdPresenter presenter;
    @Inject
    ResetPwdBiz biz;

    private TimerUtil timerUtil;

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_reset_pwd);

    }

    @Override
    protected void setup() {
        setStateText();
        setToolBar("重置密码", "");
        DaggerResetPwdComponent.builder().resetPwdModule(new ResetPwdModule(this)).build().inject(this);
        presenter.setBiz(biz);

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        timerUtil = new TimerUtil(59000, 1000, getCode);
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.ForgetPwd(phone.getText().toString().trim(), pwd.getText().toString().trim(), pwdSure.getText().toString().trim(), code.getText().toString());
                    }
                });
        RxView.clicks(getCode)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.getCode(phone.getText().toString());
                    }
                });

    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }
    /***
     * Stop location service
     */
    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();

    }

    @Override
    public void countDown() {
        timerUtil.start();
    }
}
